package com.ruoyi.laiqianba.http;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.laiqianba.config.AdminConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class AdminHttpUtil {

    @Autowired
    AdminConfig adminConfig;

//    //    public static final String domain = "https://api.koukoudai.shop/"; //统一接口域名，测试环境
//    public static final String domain = "http://127.0.0.1:1779/"; //统一接口域名，测试环境
//
//    public static final String appId = "admin";
//    public static final String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAutf3AIXEKDOk/zhLDGe4EL1b62N4ZvLn2ZvU+yjMyh7xNBYYryBU8qmKc9Czxc1QHMrI56KNxslCfW9gHR5Too/RmySH/c2R0KilwVnU8qFhUsGY3cBxEdW8k4sy5f22WSh9NbNfiRi8Kv3bSbcc3Z4wcc3tyGIlHe1sLfhL6tdrrU1u43aw63ZfcueFAPlpaMS7IjtzSMdRAylbl1Y8hDMEGFeEMY1NzgBf9ohL20Sssnq5de4S6378+PrswY5gm1jlEMSmIqmAohQL+abWR1iPj0wHlhMLMlg8h3ZA7ta94CwWp1BMPx3KSnDY9w197RS2tMNapupXrBXyHmjQtQIDAQAB";

    public AjaxResult httpRequest(String uri, Map<String , Object> bizMap){
        String requestUrl = adminConfig.getDomain() + uri;
//        String requestUrl = domain + uri;

        String aesKey = RandomUtil.randomStringUpper(32);
        String aesIv = RandomUtil.randomString(16);
        aesKey = DigestUtil.md5Hex(aesKey, "UTF-8");

        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, aesKey.getBytes(), aesIv.getBytes());
        String tranData = aes.encryptBase64(JSON.toJSONString(bizMap));

        JSONObject keyObject = new JSONObject();
        keyObject.put("key", aesKey);
        keyObject.put("iv", aesIv);
        String ciphertext = SecureUtil.rsa(null, adminConfig.getPubKey()).encryptBase64(JSON.toJSONString(keyObject), KeyType.PublicKey);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", adminConfig.getAppId());
        paramMap.put("version", "1.0.1");
        paramMap.put("ciphertext", ciphertext);
        paramMap.put("tranData", tranData);

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("randomUuid", RandomUtil.randomString(32));
        headerMap.put("channelCode", "default");
        headerMap.put("timestamp", String.valueOf(System.currentTimeMillis()));

        log.info("【请求服务后台】开始请求【requestUrl】{}【paramMap】{}【headerMap】{}", requestUrl, JSON.toJSONString(paramMap), JSON.toJSONString(headerMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(paramMap))
                .headerMap(headerMap, true)
                .execute()
                .body();
        log.info("【请求服务后台】结束请求【response】{}", response);

        JSONObject jsonObject = JSON.parseObject(response);
        String rspCode = jsonObject.getString("rspCode");
        String rspMsg = jsonObject.getString("rspMsg");
        if (StrUtil.equals(rspCode, "0001")){
            JSONObject data = jsonObject.getJSONObject("data");
            return AjaxResult.success(data);
        }
        return AjaxResult.error(rspMsg);
    }

}
